/**   
* @Title: MainProjectCreateLogic.java 
* @Package cn.com.gome.generator.logic 
* @Description: 主项目创建
* @author chenmin-ds   
* @date 2017年5月26日 下午2:49:05 
* @company cn.com.gome
* @version V1.0   
*/ 


package cn.com.gome.generator.logic;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.com.gome.framework.dao.entity.TblProjectBasicInfo;

import com.gomeplus.frame.exception.LogicsException;
import com.gomeplus.frame.logic.ILogics;
import com.gomeplus.frame.logic.ResultEnum;

/** 
 * @ClassName: MainProjectCreateLogic 
 * @Description: 主项目创建
 * @author chenmin-ds 
 * @date 2017年5月26日 下午2:49:05  
 */
@Service
public class MainProjectCreateLogic implements ILogics<TblProjectBasicInfo> {

	private Logger logger = LoggerFactory.getLogger("MainProjectCreateLogic");
	
	public ResultEnum exec(TblProjectBasicInfo projectBasicInfo) throws LogicsException {
		try{
			String projectPath = projectBasicInfo.getProjectPath().trim();
			if(!projectPath.endsWith("//")){
				projectPath +="//";
			}
			
			File file = new File(projectBasicInfo.getProjectPath().trim());
			if(!file.exists()){
				file.mkdirs();
			}
			
			projectPath += projectBasicInfo.getProjectEng();
			file = new File(projectPath);
			if(!file.exists()){
				file.mkdir();
			}
			
			projectPath += "//pom.xml";
			file = new File(projectPath);
			if(!file.exists()){
				file.createNewFile();
			}
			
			StringBuffer sb = new StringBuffer();
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			sb.append("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
			sb.append("	xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n");
			sb.append("	<modelVersion>4.0.0</modelVersion>\n");
			sb.append("	<groupId>"+projectBasicInfo.getPackages()+"</groupId>\n"); 
			sb.append("	<artifactId>" + projectBasicInfo.getProjectEng() + "</artifactId>\n");
			sb.append("	<version>0.0.1-SNAPSHOT</version>\n"); 
			sb.append("	<packaging>pom</packaging>\n");
			sb.append("	<name>" + projectBasicInfo.getProjectEng() + "</name>\n");
			sb.append("	<url>http://maven.apache.org</url>\n");
			sb.append("	<properties>");
			sb.append("		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n");
			sb.append("		<spring.version>3.2.8.RELEASE</spring.version>\n");
			sb.append("	</properties>\n");
			sb.append("	<dependencies>\n");
			sb.append("		<dependency>\n");
			sb.append("			<groupId>junit</groupId>\n");
			sb.append("			<artifactId>junit</artifactId>\n");
			sb.append("			<version>4.10</version>\n");
			sb.append("		</dependency>\n");
			sb.append("	</dependencies>\n");
			
			sb.append("	<distributionManagement>\n");
			sb.append("		<snapshotRepository>\n");
			sb.append("			<id>snapshots</id>\n");
			sb.append("			<url>http://maven.ds.gome.com.cn/nexus/content/repositories/snapshots</url>\n");
			sb.append("		</snapshotRepository>\n");
			sb.append("		<repository>\n");
			sb.append("			<id>releases</id>\n");
			sb.append("			<url>http://maven.ds.gome.com.cn/nexus/content/repositories/releases</url>\n");
			sb.append("		</repository>\n");
			sb.append("	</distributionManagement>\n");
			sb.append("	<build>\n");
			sb.append("		<plugins>\n");
			sb.append("			<plugin>\n");
			sb.append("				<artifactId>maven-source-plugin</artifactId>\n");
			sb.append("				<version>2.1</version>\n");
			sb.append("				<configuration>\n");
			sb.append("					<attach>true</attach>\n");
			sb.append("				</configuration>\n");
			sb.append("				<executions>\n");
			sb.append("					<execution>\n");
			sb.append("						<phase>compile</phase>\n");
			sb.append("						<goals><goal>jar</goal></goals>\n");
			sb.append("					</execution>\n");
			sb.append("				</executions>\n");
			sb.append("			</plugin>\n");
			sb.append("		</plugins>\n");
			sb.append("	</build>\n");
			sb.append("	<modules></modules>\n");
			sb.append("</project>");
			OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(projectPath), "UTF-8");
			BufferedWriter writer = new BufferedWriter(write);
			writer.write(sb.toString());
			writer.flush();
			writer.close();
			
			return ResultEnum.OK;
		}catch(Exception e){
			logger.error("创建主项目异常：projectEng=" + projectBasicInfo.getProjectEng(),e);
		}
		return ResultEnum.PART_CASE_01;
	}

}
